﻿using System;
using System.Globalization;
using System.IO;
using System.Text;

namespace Isotop.Text
{
	/// <summary>
	/// Implements a System.IO.TextWriter for writing information to a string. 
	/// The information is stored in an underlying Isotop.Test.LazyStringBuilder.
	/// </summary>
	[Serializable]
	public class LazyStringWriter : TextWriter
	{
		readonly LazyStringBuilder _sb;
		static UnicodeEncoding _encoding;

		// Methods
		public LazyStringWriter() : this(new LazyStringBuilder(), CultureInfo.CurrentCulture) { }
		public LazyStringWriter(IFormatProvider formatProvider) : this(new LazyStringBuilder(), formatProvider) { }
		public LazyStringWriter(LazyStringBuilder sb) : this(sb, CultureInfo.CurrentCulture) { }
		public LazyStringWriter(LazyStringBuilder sb, IFormatProvider formatProvider) : base(formatProvider)
		{
			if (sb == null)
				throw new ArgumentNullException("sb");
			_sb = sb;
		}

		public virtual LazyStringBuilder GetStringBuilder()
		{
			return _sb;
		}

		public override string ToString()
		{
			return _sb.ToString();
		}
		/*
		public override void Write(char value)
		{
			_sb.Append(value);
		}*/

		public override void Write(string value)
		{
			_sb.Append(value);
		}

		public override void Write(char[] buffer, int index, int count)
		{
			_sb.Append(new String(buffer, index, count));
		}

		public override Encoding Encoding
		{
			get
			{
				if (_encoding == null)
					_encoding = new UnicodeEncoding(false, false);
				return _encoding;
			}
		}
	}

}
